﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using DevExpress.Web.Mvc;
using Webcar.BLL;
using Webcar.Entitys;
using Webcar.Utils;
namespace Webcar.Controllers
{
    public class CommonController : BaseControler
    {
        //public ActionResult ExportTo()
        //{
            //return GridViewExtension..ExportToXls()
            //return GridViewDemosHelper.ExportTypes[typeName].Method(GridViewDemosHelper.ExportGridViewSettings, NorthwindDataProvider.GetInvoices());
                
        //} 
        public ActionResult GetChildDept(string deptCode)
        {
            var list = WebcarDataHelper.GetChildDeptByDeptList(deptCode);
            return Json(list, JsonRequestBehavior.AllowGet);
        }
        public ActionResult GetAutoCompleteCar(string deptcode, string name_startsWith)
        {
            //var list = from p in Webcar.Entitys.WebcarDataProvider.DB.BS_CAR_RECORD
            //           where (p.DEPT_CODE == deptCode || p.CHILD_DEPT == deptCode) && p.IS_USE == "T"
            //           orderby p.RECORD_NO
            //           select new {CarNo=p.CAR_NO,RecordNo=p.RECORD_NO};
            
            var list = from p in Webcar.Entitys.WebcarDataProvider.DB.BS_CAR_RECORD
                       join p2 in CurrentUser.UserGrantedDept
                       on p.CHILD_DEPT equals p2.deptcode
                       where p.DEPT_CODE == deptcode && p.IS_USE=="T"
                       && p.RECORD_NO.Contains(name_startsWith.Trim())
                       orderby p.RECORD_NO
                       select new {
                           CAR_NO=p.CAR_NO,
                           RECORD_NO = p.RECORD_NO ,
                           CAPTION = p.RECORD_NO + (p.CAR_SORT == "52"?"(摩托车)":"")+"--" + p2.deptname 
                       };
            return Json(list.Take(10).ToArray(), JsonRequestBehavior.AllowGet);
        }
        public ActionResult GetAutoCompleteDriver(string name_startsWith,string dept_code=null)
        {
            var query = from p in WebcarDataProvider.DB.BS_EMP
                    join
                        p2 in CurrentUser.UserGrantedDept
                        on p.DEPT_CODE equals p2.deptcode
                        where p.MAN_NAME.Contains(name_startsWith.Trim()) && p.IS_USE == "T"
                    orderby p.MAN_NAME
                    select new
                    {
                        MAN_CODE = p.MAN_CODE,
                        MAN_NAME = p.MAN_NAME,
                        deptcode=p2.deptcode,
                        CAPTION = p.MAN_NAME + "--" + p2.deptname
                    };
            if (dept_code.IsNotNull())
            {
                query = query.Where(a => a.deptcode == dept_code).Union(query.Where(a => a.deptcode != dept_code));
            }
            //var list = query.Where(as=>as.)
            return Json(query.Take(15).ToArray(), JsonRequestBehavior.AllowGet);
        }
        public ActionResult RetriveCarInfo(string carNo)
        {
            var car = WebcarDataProvider.DB.BS_CAR_RECORD.Find(carNo);
            return Json(car, JsonRequestBehavior.AllowGet);
        }
        public ActionResult RetrieveCarOil(string car_no,string month)
        {
            var mile = WebcarDataProvider.DB.BS_CAR_MILE.Find(car_no, month);
            if (mile != null)
                return Content(mile.FUEL_NUM.ToString());
            else
                return Content("0");
        }
        public ActionResult RetrieveCarMile(string car_no, string month)
        {
            try
            {
                string last_month = (month + "-01").ToDateTime().AddMonths(-1).ToString("yyyy-MM");
                var mile = WebcarDataProvider.DB.BS_CAR_MILE.Find(car_no, last_month);
                if (mile != null)
                    return Content(mile.READ_MILE.ToString());
                else
                    return Content("0");
            }
            catch
            {
                return Content("0");
            }

        }

        public ActionResult QueryCalendarHtml(string name)
        {
            string format ="yyyy-MM-dd";
            DateTime date = DateTime.Now;
            string html = "<input type=\"text\" id=\"" + name + "\" name=\"" + name + "\" onfocus=\"WdatePicker({dateFmt:'" + format + "'})\" class=\"Wdate\" style=\"width:96%\" value=\"" + date.ToString(format) + "\" />";
            return Content(html);
        }
        public ActionResult QueryText(string name)
        {
            return Content("<input id='" + name + "' name='"+name+"' type='text' />");
        }
        public ActionResult QueryYesNo(string name)
        {
            return QueryDropdownList(name, new SelectList(WebcarDataHelper.YesNoCode, "Item1", "Item2"));
        }
        public ActionResult QueryCarSort(string name)
        {
            return QueryDropdownList(name, WebcarDataHelper.CarSortDropDown);
        }
        public ActionResult QuerySecType(string name)
        {
            return QueryDropdownList(name, new SelectList(WebcarDataHelper.SecType, "Item1", "Item2"));
        }
        public ActionResult QuerySecKind(string name)
        {
            return QueryDropdownList(name, new SelectList(WebcarDataHelper.SecKind, "Item1", "Item2"));
        }
        public ActionResult QueryRepairKind(string name)
        {
            return QueryDropdownList(name, new SelectList(WebcarDataHelper.RepairKind, "Item1", "Item2"));
        }

        public ActionResult QuerySecDuty(string name)
        {
            return QueryDropdownList(name, new SelectList(WebcarDataHelper.SecDuty, "Item1", "Item2"));
        }


        public ActionResult QueryJobKind(string name)
        {
            return QueryDropdownList(name, new SelectList(WebcarDataHelper.JobKind, "Item1", "Item2")); 
        }

        ActionResult QueryDropdownList(string name,SelectList list)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<select id=\"" + name + "\" name=\"" + name + "\"");
            sb.Append("<option value=\"\">--请选择--</option>");
            foreach (var item in list)
            {
                sb.Append("<option value=\"{0}\">{1}</option>".FormatWith(item.Value,item.Text));
            }
            sb.Append("</select>");

            return Content(sb.ToString());
        }

    }
}
